#!/bin/bash
#
# release.sh: build release
#
# USAGE: release.sh TAG RELEASE-VERSION
#
#   TAG is the SVN tag of the release
#
#   RELEASE-VERSION is the name that will be given to the tarball.
#   Eg. if RELEASE-VERSION is v3_0_3 then the tarball will be fusionregistry-v3_0_3.tar.gz
#
#
# $Id$
#

if [ $# -lt "2" ]
then
  echo
  echo "Usage: $0 TAG RELEASE-VERSION"
  echo "  see the comments in this script for more info."
  echo
  exit 1
fi

# This forces all exported releases to be on the same timezone
# when SVN props are used inside files.
TZ=UTC

TAG=$1
RELEASE_VERSION=$2
REPOS_NAME="fusionregistry"

dirname="${REPOS_NAME}_${RELEASE_VERSION}"

echo
echo "       Tag name: $TAG"
echo "      Directory: $dirname"

if test -d ${dirname}; then
  echo "ERROR: for safety, you must manually remove $dirname."
  echo
  exit 1
fi

split="---------------------------------------------------------------------"

# make sure that the perms are good for the tarball
umask 022

echo
echo $split
echo
echo "Starting SVN export of ${REPOS_NAME} to $dirname ..."
echo
svn export https://fusionregistry.googlecode.com/svn/tags/${TAG} ${dirname}

echo
echo $split
echo
echo "Eliminating unwanted files (e.g. .cvsignore) ..."

find $dirname -name .cvsignore | xargs rm -f

echo
echo $split
echo
echo "Building the tarball, .gz, .bz2 and zip files ..."

tar cf ${dirname}.tar ${dirname}
gzip -9 --to-stdout ${dirname}.tar > ${dirname}.tar.gz
bzip2 -9 -c ${dirname}.tar > ${dirname}.tar.bz2
rm -f ${dirname}.tar
zip -9 -r ${dirname}.zip ${dirname}

echo $split
echo
echo "Cleaning up  ..."
echo

rm -rf ${dirname}

echo
echo $split
echo
echo "Generating md5sums ..."

rm -f ${dirname}.md5

ARCH_FORMATS="tar.gz tar.bz2 zip"

if test -x "`which md5sum 2> /dev/null`"; then
  for x in ${ARCH_FORMATS}
  do
    md5sum ${dirname}.${x} >> ${dirname}.md5
  done
fi

echo
echo "Completed!"
echo
